import Common from '../../common/common.js';
import BaseListItem from '../base/BaseListItem.js';
import TimNet from '../../timnet.js';
import ServiceNet from '../../servicenet.js';
import Friend from './Friend.js';
import {FriendInfoWin} from './FriendWin.js';
import Talk from "../conversation/Talk.js";

//寄生组合式继承
function FriendListItem (data) {
    BaseListItem.call(this,data);
 }
let prototype = Object.create(BaseListItem.prototype);
prototype.constructor = FriendListItem;
FriendListItem.prototype  = prototype;

//操作
FriendListItem.prototype.initHandleData = function () {
    return [
        {label:"span",tag:"del",props:{className:"del"}},
        {label:"span",tag:"blacklist",props:{className:"blacklist"}}
        ];
}

//各个操作事件
FriendListItem.prototype.bind = function(){
    Common.tool.addHandle(this.element.handle.del,"click",()=>{
        if(window.confirm("确定要删除此好友?")){
            ServiceNet.delFriend(this.data.userID,()=>{
                 Friend.initData();
            });
        }
    });
    Common.tool.addHandle(this.element.handle.blacklist,"click",()=>{
        if(window.confirm("确定要将此好友移入黑名单?")){
            TimNet.addToBlacklist([this.data.userID], (data) => {
                 Friend.initData();
            });
        }
    });

    Common.tool.addHandle(this.element.avatarDiv,"click",()=>{
        new FriendInfoWin(this.data).create();
    });

    Common.tool.addHandle(this.element.infoDiv,"click",()=>{

        Talk.openByID("C2C"+this.data.userID);
    });

}

//设置数据
FriendListItem.prototype.setData = function(data){
    this.data = data;
    this.setAvatar(Common.tool.getAvatar(data.avatar));
    this.setName(Common.tool.isNull(data.nick)?data.userID:data.nick);
    this.setMsg(data.selfSignature);
}

//设置用户
FriendListItem.prototype.updateDataByUserID = function(userID,remark){
    //如果用户存在
    if(Common.cache.userInfo[userID]){
        Common.cache.userInfo[userID].remark = remark;
        this.setData(Common.cache.userInfo[userID]);
     }
    else
    {
        TimNet.getUserProfile([userID],(d)=>{
            if(d && d[0]){
                Common.cache.userInfo[userID] = d[0];
                Common.cache.userInfo[userID].remark = remark;
                this.setData(Common.cache.userInfo[userID]);
             };
        })
    }
    return this;
}

export default FriendListItem
